    //
//  LoginView.m
//  iFlower
//
//  Created by Hoang Thuc on 15/07/2010.
//  Copyright 2010 iNIL. All rights reserved.
//

#import "LoginView.h"
#import "User.h"
#import "Request.h"
#import "iFlowerAppDelegate.h"
#import "Order.h"
#import "RegisterView.h"

@implementation LoginView

@synthesize user_id;
@synthesize password;
- (void) contentLoaded:(NSNotification*) notification
{
	NSLog(@"content loaded");
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentLoaded" object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentFailed" object:nil];
	[[iFlowerAppDelegate shareDelegate] hideLoadingView];
	NSDictionary* dictionary = [[[Request sharedRequest] entries] objectAtIndex:0];
	NSString* status= [dictionary objectForKey:@"status"];
	NSLog(@"status:%@",status);
	if([status isEqualToString:@"OK"])
	{
		User* user = [User new] ;
		user.username = self.user_id.text;
		user.password = self.password.text;
		NSDictionary* userdata = [dictionary objectForKey:@"userdata"];
		user.firstName = [userdata objectForKey:@"firstname"];
		user.lastName = [userdata objectForKey:@"lastname"];
		user.tittle = [userdata objectForKey:@"title"];
		user.company = [userdata objectForKey:@"company"];
		user.city = [userdata objectForKey:@"city"];
		user.email = [userdata objectForKey:@"email"];
		user.phone = [userdata objectForKey:@"phone"];
		user.county = [userdata objectForKey:@"county"];
		user.country = [userdata objectForKey:@"country"];
		user.address = [userdata objectForKey:@"address"];
		user.state = [userdata objectForKey:@"state"];
		user.zipcode = [userdata objectForKey:@"zipcode"];
		user.shippingFirstName = [userdata objectForKey:@"s_firstname"];
		user.shippingLastName = [userdata objectForKey:@"s_lastname"];
		user.shippingTittle = [userdata objectForKey:@"s_title"];
		user.shippingCompany = [userdata objectForKey:@"s_company"];
		user.shippingCity = [userdata objectForKey:@"s_city"];
		user.shippingCounty = [userdata objectForKey:@"s_county"];
		user.shippingCountry = [userdata objectForKey:@"s_country"];
		user.shippingAddress = [userdata objectForKey:@"s_address"];
		user.shippingState = [userdata objectForKey:@"s_state"];
		user.shippingZipcode = [userdata objectForKey:@"s_zipcode"];
		user.billingTittle = [userdata objectForKey:@"b_title"];
		user.billingFirstName = [userdata objectForKey:@"b_firstname"];
		user.billingLastName = [userdata objectForKey:@"b_lastname"];
		user.billingCity = [userdata objectForKey:@"b_city"];
		user.billingCounty = [userdata objectForKey:@"b_county"];
		user.billingCountry = [userdata objectForKey:@"b_country"];
		user.billingAddress = [userdata objectForKey:@"b_address"];
		user.billingZipcode = [userdata objectForKey:@"b_zipcode"];
		user.billingState = [userdata objectForKey:@"b_state"];
		user.billingCompany = [userdata objectForKey:@"b_company"];
		[[iFlowerAppDelegate shareDelegate] setUser:user];
		[[[iFlowerAppDelegate shareDelegate] order] setUser:user];
		[[iFlowerAppDelegate shareDelegate] setLogined:YES];
		[self.navigationController popViewControllerAnimated:YES];
	}else if ([status isEqualToString:@"disabled"]){
		UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"User disabled" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
		[alert show];
	}else if ([status isEqualToString:@"incorrect"]){
		UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Username and password is not match" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
		[alert show];
	}
	
}
- (void) contentFailed:(NSNotification*) notification
{
	NSLog(@"content failed");
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentLoaded" object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentFailed" object:nil];
	[[iFlowerAppDelegate shareDelegate] hideLoadingView];
}

-(IBAction) buttonMenu
{
	[self.navigationController popViewControllerAnimated:YES];
}
- (void) applicationWillResignActive:(NSNotification*) notification
{
	if ([user_id isFirstResponder]) {
		[user_id resignFirstResponder];
	}else if ([password isFirstResponder]) {
		[password resignFirstResponder];
	}
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"applicationWillResignActive" object:nil];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillResignActive:) name: @"applicationWillResignActive" object: nil];
}	
-(IBAction) buttonRegister
{
	RegisterView * registerView = [[RegisterView alloc] initWithNibName:@"RegisterView" bundle:nil];
	[self.navigationController pushViewController:registerView animated:YES];
	[registerView release];
}
-(IBAction) buttonLogin
{
	if([self.user_id.text length]==0)
	{
		UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Please enter username" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
		[alert show];
		[self.user_id becomeFirstResponder];
		return;
	}
	if([self.password.text length]==0)
	{
		UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Please enter password" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
		[alert show];
		[self.password becomeFirstResponder];
		return;
	}
	if([self.user_id isFirstResponder])
	{
		[self.user_id resignFirstResponder];
	}
	if([self.password isFirstResponder])
	{
		[self.password resignFirstResponder];
	}
	User* user = [[User new] autorelease];
	user.username = self.user_id.text;
	user.password = self.password.text;
	[[Request sharedRequest] login:user];
	[[iFlowerAppDelegate shareDelegate] showLoadingView];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentLoaded:) name: @"ContentLoaded" object: nil];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentFailed:) name: @"ContentFailed" object: nil];
}

//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
//{
//	return YES;
//}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
	[user_id resignFirstResponder];
	[password resignFirstResponder];
	[super touchesBegan:touches withEvent:event];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
	[UIView beginAnimations:nil context:NULL];
	self.view.frame = CGRectMake(self.view.frame.origin.x, 
								 self.view.frame.origin.y - 60, 
								 self.view.frame.size.width,
								 self.view.frame.size.height);
	[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
	[UIView beginAnimations:nil context:NULL];
	self.view.frame = CGRectMake(self.view.frame.origin.x, 
								 self.view.frame.origin.y + 60, 
								 self.view.frame.size.width,
								 self.view.frame.size.height);
	[UIView commitAnimations];
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[user_id release];
	[password release];
    [super dealloc];
}


@end
